﻿Imports System.IO
Public Class AuctionItemsCSVAdapter


    Public Shared Sub Do_Export(ByVal oScanDataItemList As System.Collections.Generic.List(Of AuctionItem), ByVal _FileSave As String)
        Dim sFileData As String = String.Empty
        sFileData = Get_CSVRESULTS(oScanDataItemList)
        File.WriteAllText(_FileSave, sFileData)
    End Sub

    Private Shared Function Get_CSVRESULTS(ByVal oScanDataItemList As System.Collections.Generic.List(Of AuctionItem)) As String
        Dim oSB As New System.Text.StringBuilder
        Try
            With oSB
                .Append("ServerName,")
                .Append("FactionName,")
                .Append("ITEM_NAME,")
                '.Append(oAucItem.RAW_LINK & ",")
                .Append("ITEM_LEVEL,")
                .Append("ITEM_TYPE,")
                .Append("ITEM_SUBTYPE,")
                .Append("ITEMSLOT,")
                .Append("STACK_PRICE,")
                .Append("ITEM_COUNT,")
                .Append("UNIT_PRICE,")
                .Append("ITEM_MINBID,")
                .Append("ITEM_MININC,")
                .Append("ITEM_BUYOUT,")
                .Append("ITEM_CURBID,")
                .Append("ITEM_SELLER,")
                .Append("SCAN_DATE")
                .Append(vbCrLf)
            End With

            For Each oAucItem As AuctionItem In oScanDataItemList
                With oSB
                    .Append(oAucItem.ServerName & ",")
                    .Append(oAucItem.FactionName & ",")
                    .Append(oAucItem.ITEM_NAME & ",")
                    '.Append(oAucItem.RAW_LINK & ",")
                    .Append(oAucItem.ITEM_LEVEL.ToString & ",")
                    .Append(oAucItem.ITEM_TYPE & ",")
                    .Append(oAucItem.ITEM_SUBTYPE & ",")
                    .Append(GET_ITEMSLOT(oAucItem.IEQUIP) & ",")
                    .Append(oAucItem.STACK_PRICE & ",")
                    .Append(oAucItem.ITEM_COUNT & ",")
                    .Append(oAucItem.STACK_PRICE / oAucItem.ITEM_COUNT & ",")
                    .Append(oAucItem.ITEM_MINBID & ",")
                    .Append(oAucItem.ITEM_MININC & ",")
                    .Append(oAucItem.ITEM_BUYOUT & ",")
                    .Append(oAucItem.ITEM_CURBID & ",")
                    .Append(oAucItem.ITEM_SELLER & ",")
                    .Append(oAucItem.SCAN_DATE)
                    .Append(vbCrLf)
                End With
            Next
            Return oSB.ToString
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function

    Private Shared Function GET_ITEMSLOT(ByVal iSlot As Integer) As String
        Select Case iSlot
            Case -1
                Return "Non-Equipable Item"
            Case 1
                Return "Head Slot"
            Case 2
                Return "Neck Slot"
            Case 3
                Return "Shoulder Slot"
            Case 4
                Return "Body Slot"
            Case 5
                Return "Chest Slot"
            Case 6
                Return "Waist Slot"
            Case 7
                Return "Leg Slot"
            Case 8
                Return "Feet Slot"
            Case 9
                Return "WRIST"
            Case 10
                Return "HAND"
            Case 11
                Return "FINGER"
            Case 12
                Return "TRINKET"
            Case 13
                Return "WEAPON"
            Case 14
                Return "SHIELD"
            Case 15
                Return "RANGEDRIGHT"
            Case 16
                Return "CLOAK"
            Case 17
                Return "2HWEAPON"
            Case 18
                Return "BAG"
            Case 19
                Return "TABARD"
            Case 20
                Return "ROBE"
            Case 21
                Return "WEAPONMAINHAND"
            Case 22
                Return "WEAPONOFFHAND"
            Case 23
                Return "HOLDABLE"
            Case 24
                Return "AMMO"
            Case 25
                Return "THROWN"
            Case 26
                Return "RANGED"
            Case Else
                Return "Non-Equipable Item"
        End Select
    End Function


    Private Shared Function GET_ITEMUNITPRICE(ByVal STACK_PRICE As Long, ByVal ITEM_COUNT As Long) As String
        Dim iResult As Integer = STACK_PRICE / ITEM_COUNT
        Return get_pricestring(iResult)
    End Function


    Public Shared Function get_pricestring(ByVal oNum As Integer) As String
        Dim sCopper As String = String.Empty
        Dim sSilver As String = String.Empty
        Dim sGold As String = String.Empty
        Dim sTemp As String = String.Empty
        Dim oSBValue As New System.Text.StringBuilder
        'string is reversed no cc:ss:ggggg......
        sTemp = ReverseString(oNum.ToString)
        If sTemp.Length >= 2 Then
            sCopper = ReverseString(Strings.Left(sTemp, 2))
        ElseIf sTemp.Length >= 1 And sTemp.Length <= 2 Then
            sCopper = "0" & sTemp
        End If

        If sTemp.Length >= 4 Then
            sSilver = ReverseString(sTemp.Substring(2, 2))
        ElseIf sTemp.Length >= 3 Then
            sSilver = ReverseString(sTemp.Substring(2, 1))
        End If

        If sTemp.Length >= 5 Then
            sGold = ReverseString(sTemp.Substring(4, sTemp.Length - 4))
        End If

        If sGold <> String.Empty Then
            oSBValue.Append(sGold & " g: ")
        End If

        If sSilver <> String.Empty Then
            oSBValue.Append(sSilver & " s: ")
        End If

        If sCopper <> String.Empty Then
            oSBValue.Append(sCopper & " c")
        End If
        Return oSBValue.ToString
    End Function

    Public Shared Function ReverseString(ByVal targetString As String) As String
        Dim Characters() As Char = targetString.ToCharArray
        Array.Reverse(Characters)
        Return Characters
    End Function

End Class
